<?php

namespace App\Http\Controllers\Admin;

use App;
use App\Http\Controllers\Controller as BaseController;
use App\Http\Logic\Admin\RoleLogic;
use App\Common\Status;
use Exception;
use App\Http\Requests\RoleRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class RoleController extends BaseController
{

    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function list(Request $request): JsonResponse
    {
        $params   = $request->query();
        $pageSize = $request->query("pageSize", Status::PAGE_SIZE);
        $page     = $request->query("page", Status::PAGE);
        $data     = RoleLogic::getRoleList($params,$pageSize, $page);
        return $this->success(Status::SUCCESS, 'success', $data);
    }


    /**
     * @param RoleRequest $request
     * @return JsonResponse
     * @throws Exception
     */
    public function createRole(RoleRequest $request): JsonResponse
    {
        $params = $request->validated();
        $id     = RoleLogic::createRole($params);
        $r      = $params['id'] ? [] : ['id' => $id];
        return $this->success(Status::SUCCESS, 'success', $r);
    }


    /**
     * @param $id
     * @return JsonResponse
     */
    public function dtlRole($id): JsonResponse
    {
        $info = RoleLogic::getRoleDtl($id);
        return $this->success(Status::SUCCESS, 'success', $info);
    }


    /**
     * @param $id
     * @return JsonResponse
     */
    public function delRole($id): JsonResponse
    {
        if($id == 1) {
            return $this->error(Status::ERROR, '无权删除');
        }
        RoleLogic::delRole($id);
        return $this->success(Status::SUCCESS, 'success', []);
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function bindRole(Request $request): JsonResponse
    {
        $params   = $request->post();
        if(!isset($params['role_id']) || !$params['role_id'] || !isset($params['menu_id']) || !$params['menu_id']) {
            return $this->error(Status::ERROR, '参数有误');
        }
        RoleLogic::bindRole($params);
        return $this->success(Status::SUCCESS, 'success', []);
    }


    /**
     * @param $id   角色id
     * @return JsonResponse
     */
    public function bindRoleDtl($id): JsonResponse
    {
       $r = RoleLogic::getBindRoleDtl($id);
        return $this->success(Status::SUCCESS, 'success', $r);
    }

    /**
     * @param $id 用户id
     * @return JsonResponse
     */
    public function bindAdminRoleDtl($id): JsonResponse
    {
       $r = RoleLogic::getBindAdminRoleDtl($id);
        return $this->success(Status::SUCCESS, 'success', $r);
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function bindAdminRole(Request $request): JsonResponse
    {
        $params   = $request->post();
        if(!isset($params['role_id']) || !$params['role_id'] || !isset($params['admin_id']) || !$params['admin_id']) {
            return $this->error(Status::ERROR, '参数有误');
        }
        RoleLogic::bindAdminRole($params);
        return $this->success(Status::SUCCESS, 'success', []);
    }

}
